package com.kuangjia.student_ems.controller;

import com.kuangjia.student_ems.dto.AiChatRequest;
import com.kuangjia.student_ems.dto.AiChatResponse;
import com.kuangjia.student_ems.service.AiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.logging.Logger;

/**
 * DeepSeek AI API控制器
 * 处理AI聊天请求并返回响应
 */
@RestController
@RequestMapping("/api/ai")
public class AiController {

    private static final Logger logger = Logger.getLogger(AiController.class.getName());

    @Autowired
    private AiService aiService;

    /**
     * 处理聊天请求
     * @param request 包含用户问题的请求对象
     * @return AI回复内容
     */
    @PostMapping("/chat")
    public ResponseEntity<AiChatResponse> chat(@RequestBody AiChatRequest request) {
        try {
            logger.info("收到AI聊天请求: " + request.getQuestion());
            AiChatResponse response = aiService.generateResponse(request.getQuestion());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.severe("处理AI聊天请求时发生错误: " + e.getMessage());
            return ResponseEntity.internalServerError().body(
                new AiChatResponse("抱歉，处理您的请求时出现了问题，请稍后再试。")
            );
        }
    }
}